<template>
    <div>
        <!-- 功能按钮 -->
        <div style="margin-bottom: 10px; margin-top: 5px">
            <el-input placeholder="搜索品牌" @input="updateData" @clear="updateData" v-model="search" clearable
                style="width:250px" prefix-icon="el-icon-search"></el-input>
            <el-button style="margin-left:10px" type="primary" size="small" @click="updateData()">搜索</el-button>

            <el-button size="small" type="success" @click="dialogFormVisible = true">添加品牌<i
                    class="el-icon-circle-plus-outline"></i>
            </el-button>
        </div>

        <!--弹窗表单-->
        <el-dialog title="汽车信息表单" :visible.sync="dialogFormVisible" width="50%" >
            <el-form label-width="80px" :model="ruleForm" status-icon :rules="rules" ref="ruleForm"
                class="demo-ruleForm">
                <el-form-item label="品牌名称" prop="brandName">
                    <el-input v-model="ruleForm.brandName" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="品牌标志" prop="brandLogo">
                    <el-upload class="avatar-uploader" action="http://localhost:8082/file/upload"
                        :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                        <img v-if="ruleForm.brandLogo" :src="ruleForm.brandLogo" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>

                </el-form-item>
                <el-form-item label="品牌简介" prop="brandIntro">
                    <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" placeholder="请输入内容"
                        v-model="ruleForm.brandIntro">
                    </el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveBrand('ruleForm')">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="删除模式选择" width="30%">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="saveBrand('ruleForm')">确 定</el-button>
        </el-dialog>

        <!-- 表格渲染 -->
        <el-table :data="tableData" border stripe style="width: 1300px;" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55">
            </el-table-column>

            <el-table-column align="center" prop="brandLogo" label="品牌图标" width="100">
                <template v-slot:default="scope">
                    <el-image :src="scope.row.brandLogo" />
                </template>
            </el-table-column>

            <el-table-column align="center" prop="brandName" label="品牌名称">
            </el-table-column>

            <el-table-column align="center" prop="brandIntro" label="品牌简介">
            </el-table-column>
            <el-table-column align="center" prop="brandTemp" label="品牌热度" >
            </el-table-column>

            <el-table-column label="操作" align="center" width="200px">
                <template slot-scope="scope">
                    <el-button type="success" @click="editStore(scope.row)">编辑<i class="el-icon-edit"></i></el-button>

                    <el-popconfirm confirm-button-text='确定' cancel-button-text='取消' icon="el-icon-info" icon-color="red"
                        title="您确定删除吗？" @confirm="deleteStore(scope.row.brandId)" style="margin-left:5px">
                        <el-button type="danger" slot="reference">删除<i class="el-icon-remove-outline"></i>
                        </el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div style="padding:10px 0;">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="currentPage" :page-sizes="[5, 10, 15, 20]" :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
export default {
    name: "Brand",
    data() {
        return {
            //表单验证规则
            ruleForm: {
                brandName: '',
                brandIntro: '',
                brandLogo: ''
            },
            rules: {
                brandName: [{ required: true, message: '请输入品牌名称', trigger: 'blur' },],
                brandIntro: [{ required: true, message: '请输入品牌简介', trigger: 'blur' },],
                brandLogo: [{ required: true, message: '请上传品牌标志', trigger: 'blur' }]
            },
            tableData: [],
            currentPage: 1,
            pageSize: 5,
            search: "",
            total: 0,
            dialogFormVisible: false,
            form: {}, //弹窗表单内容,
            imageUrl: '',
            title:"添加品牌"
        }
    },
    methods: {
        /* 图片上传相关 */
        handleAvatarSuccess(res, file) {
            //debugger;
            this.ruleForm.brandLogo = res.data;
            this.$message.success('上传成功');
            //URL.createObjectURL(file.raw);
        },

        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isPNG = file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG && !isPNG) {
                this.$message.error('上传头像图片只能是 JPG 或 PNG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return (isJPG || isPNG) && isLt2M;
        },


        handleSizeChange(val) {
            this.pageSize = val;
            console.log(`每页 ${val} 条`);
            this.updateData();
        },
        handleCurrentChange(val) {
            this.currentPage = val;
            console.log(`当前页: ${val}`);
            this.updateData();
        },
        handleSelectionChange(val) {
            console.log(val);
            this.multipleSelection = val;
        },
        updateData() {
            this.$axios.get("/brand/page", {
                params: {
                    pageNum: this.currentPage,
                    pageSize: this.pageSize,
                    brandName: this.search
                }
            }).then(res => {
                //debugger;
                //console.log(res.data.records);
                this.tableData = res.data.data.records;
                this.total = res.data.data.total;
            });
        },
        saveBrand(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    //alert('submit!');
                    //debugger;
                    this.$axios.post("/brand/add", this.ruleForm).then(res => {
                        console.log(res)
                        if (res) {
                            this.$message.success("保存成功");
                            this.ruleForm = {};
                            this.dialogFormVisible = false;
                            this.updateData();
                        }
                        else {
                            this.$message.error("保存失败");
                        }
                    });
                } else {
                    console.log('error submit!!');
                    this.$message.error("表单填写有误，无法提交！");
                    return false;
                }
            });
        },
        deleteStore(id) {

            this.$axios.post("/brand/del",id).then(res => {
                    if (res) {
                        this.$message.success("删除成功");
                        if ((this.total - 1) % this.pageSize == 0) {
                            this.currentPage = this.currentPage > 1 ? this.currentPage - 1 : 1;
                        }
                        this.updateData();
                    }
                    else {
                        this.$message.error("删除失败，请联系系统管理员处理！");
                    }
                });
            
        },

        //编辑数据
        editStore(row) {
            //赋值
            this.ruleForm = row;
            //显示编辑框
            this.dialogFormVisible = true;
        },
        clear() {
            this.search = "";
            this.updateData();
        },
        
        
    },
    created() {
        this.updateData();
    }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}

</style>